home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / Engine / parseURL.py < prev    next >
Encoding:
Python Source  |  2005-06-01  |  5.0 KB  |  118 lines

  1. #
  2. #  parseURL.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 2/1/05.
  6. #  Copyright (c) 2005 Benjamin Han. All rights reserved.
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  
  23. #!/usr/bin/env python
  24.  
  25. import urlparse
  26.  
  27. from consts import *
  28.  
  29.  
  30. _yahooPrefixPat = re.compile(r'(?i)(?:\w+\.)?rds?\.|drs?\.')
  31. _msnPrefixPat = re.compile(r'(?i)g|ads')
  32.  
  33.  
  34. def parseURL (urlStr):
  35.     """Given an unquoted URL string in lowercase, returns the real site targetted
  36.     (a tuple of name components, e.g., ('www','yahoo','com')); returns None if parsing
  37.     failed or no site is found.
  38.  
  39.     ASSUMPTION: urlStr starts with 'http:' or 'https:'
  40.     """
  41.  
  42.     # make sure we have '//' following 'http:' or 'https://'
  43.     i = urlStr.index(':')
  44.     j = i + 2
  45.     if j < len(urlStr):
  46.         if urlStr[j] != '/':
  47.             if urlStr[j - 1] == '/':
  48.                 urlStr = '%s://%s' % (urlStr[:i], urlStr[j:])
  49.             else:
  50.                 urlStr = '%s://%s' % (urlStr[:i], urlStr[i + 1 :])
  51.     else:
  52.         return None
  53.  
  54.     t = urlparse.urlsplit(urlStr)
  55.  
  56.     site = t[1].split('@')[-1]
  57.     
  58.     # trick #1 - google:
  59.     # urlparse.urlsplit('http://www.google.com/url?q=http://www.spammer.biz')
  60.     # -> ('http', 'www.google.com', '/url', 'q=http://www.spammer.biz', '')
  61.     if site == 'www.google.com' and t[2] == '/url':
  62.         mo = httpPat.search(t[3])
  63.         if mo:            
  64.             return parseURL(mo.group(0))
  65.  
  66.     # trick #2 - yahoo:
  67.     # urlparse.urlsplit('http://rd.yahoo.com/some/junk/*http://my.real.site/hahaha.html')
  68.     # -> ('http', 'rd.yahoo.com', '/some/junk/*http://my.real.site/hahaha.html', '', '')
  69.     # Note: 'rd.yahoo.com' can be replaced with 'drs.yahoo.com', 'eur.rd.yahoo.com' or 'srd.yahoo.com'
  70.     elif site.endswith('yahoo.com') and _yahooPrefixPat.match(site[:-9]) is not None:
  71.         mo = httpPat.search(t[2])
  72.         if mo:
  73.             return parseURL(mo.group(0))
  74.  
  75.     # trick #3 - msn
  76.     # urlparse.urlsplit('http://g.msn.com/0US!s5.31472_315529/HP.1001?http://auto-warranty-quotes.com/st.html')
  77.     # -> ('http', 'g.msn.com', '/0US!s5.31472_315529/HP.1001', 'http://auto-warranty-quotes.com/st.html', '')
  78.     # Note: 'g.msn.com' can be replaced with 'ads.msn.com'
  79.     elif site.endswith('msn.com') and _msnPrefixPat.match(site[:-7]) is not None:
  80.         mo = httpPat.search(t[3])
  81.         if mo:
  82.             return parseURL(mo.group(0))
  83.  
  84.     # For some reason urlsplit() doesn't separate the query from site in this:
  85.     # 'http://www.myaffordablepills.com?rid=1000'
  86.     site = site.split(':')[0].split('?')[0].strip()
  87.  
  88.     # some rudimentary check of valid domain names
  89.     if len(site):
  90.         l = site.split('.')
  91.         if len(l[0]) == 0: del l[0]
  92.         if len(l[-1]) == 0: del l[-1]
  93.         if len(filter(lambda comp:len(comp) == 0, l)):
  94.             # can't have empty components
  95.             return None
  96.         if len(l) > 1: return tuple(l)
  97.         
  98.     return None
  99.  
  100.  
  101. if __name__ == '__main__':
  102.     print parseURL('http://user@www.cwi.nl:80/~guido/Python.html')
  103.     print parseURL('http://www.google.com/url?q=http://www.spammer.biz')
  104.     print parseURL('http://rd.yahoo.com/some/junk/*http://my.real.site1/hahaha.html')
  105.     print parseURL('http://drs.yahoo.com/some/junk/*http://my.real.site2/hahaha.html')
  106.     print parseURL('http://g.msn.com/0US!s5.31472_315529/HP.1001?http://auto-warranty-quotes.com/st.html')
  107.     print parseURL('http://9c6b:MA5U@removerequest.biz/biskit/')
  108.     print parseURL('http://www.fast-lender-search.biz/go')
  109.     print parseURL('http://www.myaffordablepills.com?rid=1000')
  110.     print parseURL('http://61.145.118.245/r13244/stats/clicks.asp?url=http://incrediblemeds.com/sv/index.php?pid=xyz123')
  111.     print parseURL('http://rd.yahoo.com/M=032344.7057012.8084439.0341594/D=yahoo_top/S=3633887:LCC/A=2287395/R=0/*http://www.getitwhileitlast.com/x/index.php?AFF_ID=x0323')
  112.     print parseURL('http://rd.yahoo.com/yearbook/calisthenic/abe/*http:/www.valuen.com/?partid=sf')
  113.     print parseURL('http://rd.yahoo.com/yearbook/calisthenic/abe/*http:www.valuen.com/?partid=sf')
  114.     print parseURL('http://ads.msn.com/ads/adredir.asp?image=/ads/IMGSFS/lwf35ysgdwsf5eo44.gif&url=http://garbage.com')
  115.     print parseURL('https://www.neteller.com/neteller/signup.cfm')
  116.     print parseURL('http://xn--fiq228c.xn--sxqt15hq1b.com')  # TO-DO: IDN is not supported right now
  117.     print parseURL('http://rds.yahoo.com/s=4611479/k=computer/v=6/sid=z/l=ws1/r=1/ss=39434998/ipc=us/she=0/h=0/sig=052yfsjo805/exp=355897284/*-http://google.com.grandi0se.net/home.asp')
  118.